﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CCImageDB
/// </summary>
/// 
namespace CE.CreditCard
{
    public class CCImageDB
    {
        public CCImageDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Update(CCImage ccimage)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCCImageUpdateSingleItem";

            SortedList _sPara = new SortedList();

            _sPara.Add("@CCId", ccimage.CCId);
            _sPara.Add("@CCImage", ccimage.CreditcardImage);
            _sPara.Add("@Status", ccimage.Status == true ? 1 : 0);

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CCImage GetCCImage(Int64 CCId)
        {
            CCImage _ccdetails = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCCSelectedImageItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CCId", CCId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _ccdetails = new CCImage();
                    _ccdetails = FillDataRecord(_dt, 0);
                }
            }
            return _ccdetails;
        }

        public CCImage GetCCImage(Int64 CCId, Int32 FinanceId)
        {
            CCImage _ccdetails = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCCSelectedImageItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CCId", CCId);
            _spara.Add("@FinancialInstitute", FinanceId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _ccdetails = new CCImage();
                    _ccdetails = FillDataRecord(_dt, 0);
                }
            }
            return _ccdetails;
        }

        private CCImage FillDataRecord(DataTable myDataTable, int RowId)
        {
            CCImage myCCImage = new CCImage();
            myCCImage.CCId = Convert.ToInt64(myDataTable.Rows[RowId]["CCId"].ToString());
            myCCImage.CardClass = Convert.ToInt32(myDataTable.Rows[RowId]["CCClass"].ToString());
            myCCImage.CreditcardImage = myDataTable.Rows[RowId]["CCImage"].ToString();
            myCCImage.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            return myCCImage;
        }

    }
}
